library Wave15a initializer init requires LegacySystem


    globals
        private trigger ChopperAITrigger = null
        private trigger FoxCompanyAITrigger = null
        private trigger FoxCompanyBradleyAI = null
        private trigger FoxCompanyInfantryAI = null
    endglobals
    
    
    
    /*
    - Marine reinforcements arrive in this Chapter. Heavy combat takes place.
    - The squad will participate in combat with Marine forces. This will be mandatory in some stages in order to survive. 
    - Constant reinforcements will arrive periodically. One can picture Dota style creep waves, with the Undead forces having a clear advantage without player intervention.
    - Level 3 Creeps make an appearance
    */

    private function InfantryOrder takes nothing returns nothing
        local unit u = GetEnumUnit()
        local real dx = GetUnitX(u) - GetLocationX(udg_CoreObjectivePoint)
        local real dy = GetUnitY(u) - GetLocationY(udg_CoreObjectivePoint)
        if GetRandomInt(0,1)==1 then
            call PolledWait(GetRandomReal(0.0,1.5))
        endif
        if udg_FollowObjective then
            if dx*dx+dy*dy<=250000 then //500 radius
                call IssueImmediateOrder(u,"holdposition")
            else
                call IssuePointOrderLoc(u,"attack",udg_CoreObjectivePoint)
            endif
        else
            call IssuePointOrder(u,"attack",udg_SquadLeaderX[3],udg_SquadLeaderY[3])
        endif
        set dx = GetUnitX(u) - udg_SquadLeaderX[3]
        set dy = GetUnitY(u) - udg_SquadLeaderY[3]
        if dx*dx+dy*dy>=1960000 then //1400 radius
            if GetUnitPointValue(u)==12 then
                call IssueImmediateOrder(u,"berserk")
            endif
            call IssuePointOrder(u,"move",udg_SquadLeaderX[3],udg_SquadLeaderY[3])
        endif
        call ItemAI(u)
        if GetUnitTypeId(u)=='z001' then
            call CorpsmanAI(u)
        endif
        set u = null
    endfunction

    private  function InfantryAI takes nothing returns nothing
        local integer i = 1
        loop
            exitwhen i>10
            if GetWidgetLife(udg_SquadLeader[i])>0.405 then
                call TriggerSleepAction(0.05)
                set udg_SquadLeaderX[3] = GetUnitX(udg_SquadLeader[i])
                set udg_SquadLeaderY[3] = GetUnitY(udg_SquadLeader[i])
                call ForGroup(udg_InfantryGroup[i],function InfantryOrder)
            endif
            set i = i+1
        endloop
    endfunction

    //===========================================================================
    function InitTrig_Infantry_AI takes nothing returns nothing
       
    endfunction


    
    //GlobalBoolean [2] - Securing LZ
    private function BradleyAI takes nothing returns nothing
        local integer i = 1
        local location l
        local group g = CreateGroup()
        local real dx
        local real dy
        local real x = GetLocationX(udg_CoreObjectivePoint)
        local real y = GetLocationY(udg_CoreObjectivePoint)
        loop
            exitwhen i>10
            if udg_FollowObjective and GetWidgetLife(udg_SquadLeader[i])>0.405 then
                set dx = GetUnitX(udg_SquadLeader[i]) - x
                set dy = GetUnitY(udg_SquadLeader[i]) - y
                if dx*dx+dy*dy<=2250000 then //1500 radius
                    call IssueImmediateOrder(udg_SquadLeader[i],"holdposition")
                else
                    if GetRandomInt(0,3)==1 then
                        call IssuePointOrder(udg_SquadLeader[i],"move",x,y)
                    else
                        call IssuePointOrder(udg_SquadLeader[i],"attack",x,y)
                    endif
                endif
            elseif GetWidgetLife(udg_SquadLeader[i])>0.405 and GetRandomInt(0,4)==1 then
                set l = GetRandomLocInRect(udg_MarineStagingArea[GetRandomInt(1,4)])
                call IssuePointOrderLoc(udg_SquadLeader[i],"move",l)
                call RemoveLocation(l)
            endif  
            set i=i+1
        endloop
        if GetWidgetLife(udg_Ogilvy)>0.405 and GetUnitState(udg_Ogilvy,UNIT_STATE_MANA)<1.0 then
            call KillUnit(udg_Ogilvy)
        endif
        set bj_groupEnumTypeId = 'h001'
        call GroupEnumUnitsOfPlayer(g , Player(11), filterGetUnitsOfTypeIdAll)
        call ForGroup(g,function CASPatrol)
        set bj_groupEnumTypeId = 'hgyr'
        call GroupEnumUnitsOfPlayer(g , Player(11), filterGetUnitsOfTypeIdAll)
        call ForGroup(g,function CASPatrol)
        call DestroyGroup(g)
        set g = null
        set l = null
    endfunction

    function SetMagnificentBastardsSpawnTriggersEnabled takes boolean b returns nothing
        if b then
            call EnableTrigger(FoxCompanyAITrigger)
            call EnableTrigger(ChopperAITrigger)
        else
            call DisableTrigger(FoxCompanyAITrigger)
            call DisableTrigger(ChopperAITrigger)
        endif
    endfunction
    
    private function AerialForces takes nothing returns nothing
        local location l = GetRandomLocInRect(udg_ItemRectangle)
        local real x = GetLocationX(l)
        local real y = GetLocationY(l)
        local group g = CreateGroup()
        call RandomSpawn('u00C',2,GetRandomInt(1,3+udg_Spawn)) //Gargoyles #3
        call RandomSpawn('u00G',1,GetRandomInt(0,3+udg_Spawn)) //Wraith #2
        if GetPlayerState(Player(11), PLAYER_STATE_RESOURCE_FOOD_USED)<30 then
            call CreateUnit(Player(11),'hgyr',x,y,0.0) //AH-1 Cobra
            call TriggerSleepAction(0.)
            call CreateUnit(Player(11),'h001',x,y,0.0) //Apache
        endif
        set bj_groupEnumTypeId = 'h001'
        call GroupEnumUnitsOfPlayer(g , Player(11), filterGetUnitsOfTypeIdAll)
        call ForGroup( g, function CASPatrol )
        set bj_groupEnumTypeId = 'hgyr'
        call GroupEnumUnitsOfPlayer(g , Player(11), filterGetUnitsOfTypeIdAll)
        call ForGroup( g, function CASPatrol )
        call DestroyGroup(g)
        set g = null
        call RemoveLocation(l)
        set l = null
    endfunction

    
    private function GroundForcesConditions takes nothing returns boolean
        return GetPlayerState(Player(13),PLAYER_STATE_RESOURCE_FOOD_USED)<90 and GetPlayerState(Player(14),PLAYER_STATE_RESOURCE_FOOD_USED)<90
    endfunction

    private function InfantryEntrance takes nothing returns nothing
        call IssuePointOrder(GetEnumUnit(),"patrol",1584.0,4520.0)
    endfunction

    private function GroundForces takes nothing returns nothing
        local integer i = GetRandomInt(1,10)
        local integer s = GetRandomInt(1,4)
        local integer A = 1
        local real x = GetRandomReal(GetRectMinX(udg_MarineSpawnArea[s]),GetRectMaxX(udg_MarineSpawnArea[s]))
        local real y = GetRandomReal(GetRectMinY(udg_MarineSpawnArea[s]),GetRectMaxY(udg_MarineSpawnArea[s]))
        local unit u
        // UE Units
        call RandomSpawn('n00D',2,GetRandomInt(1,udg_Spawn)) //Crepitus
        call RandomSpawn('n005',2,2+GetRandomInt(1,udg_Spawn)) //Zombie #3
        call RandomSpawn('n001',2,2+GetRandomInt(1,udg_Spawn)) //Immortal #2
        if udg_SquadXP>35000 then
            call RandomSpawn('uabo',1,GetRandomInt(3,6)) //Flesh Composite
        else
            call RandomSpawn('uabo',1,GetRandomInt(1,3)) //Flesh Composite
        endif
        call RandomSpawn('n00K',2,GetRandomInt(0,IMaxBJ(3,udg_Spawn))) //Blood Hound
        call RandomSpawn('z002',1,GetRandomInt(0,IMaxBJ(2,udg_Spawn))) //Slasher
        call RandomSpawn('n00A',1,GetRandomInt(1,IMaxBJ(4,udg_Players))) //Tartarus
        // Marine Units
        if GetPlayerState(Player(11),PLAYER_STATE_RESOURCE_FOOD_USED)>30 then
            return
        endif
        set udg_SquadLeader[i] = CreateUnit(Player(11),'hrtt',x,y,0.0)
        call SetUnitColor( udg_SquadLeader[i], ConvertPlayerColor(12) )
        call TriggerSleepAction(0.)
        loop
            exitwhen A>2
            set x = GetRandomReal(GetRectMinX(udg_MarineSpawnArea[s]),GetRectMaxX(udg_MarineSpawnArea[s]))
            set y = GetRandomReal(GetRectMinY(udg_MarineSpawnArea[s]),GetRectMaxY(udg_MarineSpawnArea[s]))
            set u = CreateUnit(Player(11),'z008',x,y,0.0) //Rifleman
            call GroupAddUnitSimple(u,udg_InfantryGroup[i])
            call UnitAddItemById(u,'I001') //Composite
            call SetLoadState(I2S(GetHandleId(u)),1)
            call TriggerSleepAction(0.)
            set A=A+1
        endloop
        set x = GetRandomReal(GetRectMinX(udg_MarineSpawnArea[s]),GetRectMaxX(udg_MarineSpawnArea[s]))
        set y = GetRandomReal(GetRectMinY(udg_MarineSpawnArea[s]),GetRectMaxY(udg_MarineSpawnArea[s]))
        set u = CreateUnit(Player(11),'z001',x,y,0.0) //Corpsman
        call GroupAddUnitSimple(u,udg_InfantryGroup[i])
        call UnitAddItemById(u,'I004') //Medkit
        call UnitAddItemById(u,'phea') //Fibrin
        call UnitAddItemById(u,'I001') //Composite
        call SetLoadState(I2S(GetHandleId(u)),1)
        call TriggerSleepAction(0.)
        set u = CreateUnit(Player(11),'z009',x,y,0.0) //Flamethrower
        call GroupAddUnitSimple(u,udg_InfantryGroup[i])
        call UnitAddItemById(u,'I004') //Medkit
        call UnitAddItemById(u,'I001') //Composite
        call SetLoadState(I2S(GetHandleId(u)),1)
        if GetRandomInt(0,2)==1 then
            call TriggerSleepAction(0.)
            set u = CreateUnit(Player(11),'z003',x,y,0.0) //Engineer
            call GroupAddUnitSimple(u,udg_InfantryGroup[i])
            call UnitAddItemById(u,'I005') //Reactive Armor
            call UnitAddItemById(u,'phea') //Fibrin
            call SetLoadState(I2S(GetHandleId(u)),1)
        endif
        call IssuePointOrder(udg_SquadLeader[i],"attack",1584.0,4520.0)
        call ForGroup(udg_InfantryGroup[i], function InfantryEntrance)
        set u = null
    endfunction

    private function Wave15a takes nothing returns nothing
        local unit u
        local integer i = 1
        local real trans = 0.0
        
        if udg_Players<1 or udg_Chapter==3 then
            return
        endif
        if GetRandomInt(0,10)==1 then
            set trans = 40.0
        endif
        set udg_Chapter = 3
        set udg_Intermission = true
        set udg_XPFactor = udg_XPFactor*1.2
        set udg_Lighting = true
        call SquadRatingCalc.execute()
        set udg_MarineSpawnArea[1] = gg_rct_CaravanEnd
        set udg_MarineSpawnArea[2] = gg_rct_South_Road
        set udg_MarineSpawnArea[3] = gg_rct_CaravanCEnd
        set udg_MarineSpawnArea[4] = gg_rct_West_Road
        set udg_MarineStagingArea[1] = gg_rct_TownEntranceNorth
        set udg_MarineStagingArea[2] = gg_rct_TownEntranceSouth
        set udg_MarineStagingArea[3] = gg_rct_TownEntranceEast
        set udg_MarineStagingArea[4] = gg_rct_TownEntranceWest
        set udg_LZNames[0] = "Alpha"
        set udg_LZNames[1] = "Bravo"
        set udg_LZNames[2] = "Charlie"
        set udg_LZNames[3] = "Delta"
        set udg_LZNames[4] = "Epsilon"
        set udg_LZNames[5] = "Gamma"
        set udg_ReloadText = 30
        set udg_Immortal = 'n001'
        set udg_Ghoul = 'u003'
        set udg_Parasite = 'n00J'
        call PauseAllUnitsBJ(true)
        call StartSound(gg_snd_AMTrackA)
        call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUT, 1.0, "ReplaceableTextures\\CameraMasks\\Black_mask.blp", 0, 0, 0, trans )
        call ShowInterface(false,1.0)
        call ClearTextMessages()
        call PolledWait(1.5)
        call StartSound(gg_snd_Message)
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, "                                                                          |cff8b864eMagnificent Bastards|r" )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, " " )
        call PauseAllUnitsBJ(true)
        call PolledWait(5.5)
        call CinematicFadeBJ( bj_CINEFADETYPE_FADEIN, 1.0, "ReplaceableTextures\\CameraMasks\\Black_mask.blp", 0, 0, 0, trans )
        call ShowInterface(true,1.0)
        call ClearTextMessages()
        set udg_Lighting = false
        call PolledWait(1.)
        set udg_Intermission = false
        call PauseAllUnitsBJ(false)
        call IntermissionRestore.execute()
        call PolledWait(5.)
        call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUT, 0.001, "ReplaceableTextures\\CameraMasks\\Black_mask.blp", 0, 0, 0, 0 )
        call CinematicFadeBJ( bj_CINEFADETYPE_FADEIN, 0.001, "ReplaceableTextures\\CameraMasks\\Black_mask.blp", 0, 0, 0, 0 )
        call TriggerSleepAction(0.)
        call IntermissionRestore()
        call SetForceAllianceStateBJ(bj_FORCE_PLAYER[11],udg_EchoCasualties,bj_ALLIANCE_ALLIED_VISION)
        
        if FoxCompanyInfantryAI == null then
            set FoxCompanyInfantryAI = CreateTrigger()
            call TriggerAddAction(FoxCompanyInfantryAI,function InfantryAI)
            call TriggerRegisterTimerEvent(FoxCompanyInfantryAI,10.0,true)
        endif
        
        if FoxCompanyBradleyAI == null then
            set FoxCompanyBradleyAI = CreateTrigger()
            call TriggerAddAction(FoxCompanyBradleyAI,function BradleyAI)
            call TriggerRegisterTimerEvent(FoxCompanyBradleyAI,6.0,true)
        endif
        
        if ChopperAITrigger == null then
            set ChopperAITrigger = CreateTrigger()
            call TriggerAddAction(ChopperAITrigger,function AerialForces)
            call TriggerRegisterTimerEvent(ChopperAITrigger,149.0*udg_Pace,true)
        endif

        if FoxCompanyAITrigger == null then
            set FoxCompanyAITrigger = CreateTrigger()
            call TriggerRegisterTimerEvent(FoxCompanyAITrigger,172.2*udg_Pace,true)
            call TriggerAddCondition( FoxCompanyAITrigger, Condition( function GroundForcesConditions ) )
            call TriggerAddAction(FoxCompanyAITrigger,function GroundForces)
        endif
        call EnableTrigger(FoxCompanyBradleyAI)
        call EnableTrigger(FoxCompanyAITrigger)
        call EnableTrigger(ChopperAITrigger)
        call EnableTrigger(FoxCompanyInfantryAI)
        

        call SetImmortalSpawnEnabled(true)
        call SetCrepitusSpawnEnabled(true)
        call SetGhoulSpawnEnabled(true)
        call SetDynamicEvasionEnabled(true)
        if udg_Mode==4 and udg_Players>0 then
            call ALICEText(10.0, "Chapter 2 Complete. |cff1e90ff100 XP|r awarded to " + GetPlayerName(ConvertedPlayer(udg_Sorted[1])) )
            set udg_Experience[udg_Sorted[1]] = udg_Experience[udg_Sorted[1]] + 100
        endif
	
		call DisableRareItemDropsOnDeath(Player(9))
		call DisableRareItemDropsOnDeath(Player(11))
		set i = 1
        loop
            exitwhen i>udg_InitialPlayers
			call DisableRareItemDropsOnDeath(ConvertedPlayer(udg_Sorted[i]))
            set i=i+1
        endloop

        set i = 1
        //call DisableTrigger(gg_trg_ShowStats)
        call SetSkyModel( "Environment\\Sky\\FelwoodSky\\FelwoodSky.mdl" )
        set udg_ZStart = 900
        set udg_ZEnd = 2775
        call FogTransition(0.1)
        call SetPlayerHandicap(Player(13),GetPlayerHandicap(Player(13))+0.12)
        call SetPlayerHandicap(Player(14),GetPlayerHandicap(Player(14))+0.12)
        call SetPlayerName(Player(9),"160th SOAR")
        call PlatoonReinforcements.execute()
        set u = null
        set udg_EvasionCapability = 1
        set udg_CloudFactor = udg_CloudFactor - 60
        call SetDynamicEvasionEnabled(true)
        set udg_CoreObjectivePoint = GetUnitLoc(udg_Hero[udg_Sorted[GetRandomInt(1,udg_Players)]])
        loop
            exitwhen i>udg_InitialPlayers
            call SetPlayerTechResearched(ConvertedPlayer(udg_Sorted[i]),'R00H',1)
            set i=i+1
        endloop
        call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 5.0, "0450 Local Time" )
        call PolledWait(10.0*udg_Pace)
        set udg_ZStart = 950
        set udg_ZEnd = 2800
        call FogTransition(0.15)
        set udg_CurrentWave = 15
        set udg_Visibility = 72
        call IntermissionRestore()
        call GenericActions.execute()
        call MultiboardSetTitleText( udg_SquadStatus, "Establish contact with Fox Company" )
        call MBSet( udg_SquadStatus, 1, 1, "Objective : Rendezvous with Fox Company" )
        call QuestSetDescription(udg_CurrentOrders, "Rendezvous with reactionary force. Captain Roth is the CO for Fox Company. Your squad should stay in close proximity with Fox Company to provide mutual protection and prevent either Company from being overwhelmed by Zulus." )
        call ALICE2Text(5.0, "|cff8b864eNew Objective|r : Rendezvous with Fox Company." )
        call MissionUpdate.execute()
        call PolledWait(5.)
        call GeneralText(5.0, "|cffcd950cBattalion|r : Fox Company is zeroing in on your position." )
        call PolledWait(5.)
        call GeneralText(10.0, "|cffcd950cCaptain Roth|r : Echo Company, do you hear me? We're heading to your position. Hold on." )
        call LightningAmbience.execute()
        
        call TriggerExecute(FoxCompanyAITrigger)
        
        call PolledWait(5.)
        call GeneralText(10.0, "|cffcd950cMarine|r : We hear you. We'll need a medevac for our whiskeys and kilos." )
        call PolledWait(5.)
        set udg_ZStart = 1000
        set udg_ZEnd = 2800
        call FogTransition(0.2)
        call GeneralText(10.0, "|cffcd950cCaptain Roth|r : I'm working on that." )
        call PolledWait(5.)
        call FogTransition(0.25)
        set i = GetRandomInt(1, udg_Players)
        call FieldChat(udg_Hero[udg_Sorted[i]], 5, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : We'll be fine." )
        if GetRandomInt(1,30)==1 then
            set udg_myloc = GetRandomLocInRect(udg_PlayerSpawnBoundary)
            set u = CreateUnitAtLoc(Player(13),'U00K',udg_myloc,0) //Minos
            call SuspendHeroXP(u,true)
            call RemoveLocation(udg_myloc)
        endif
        call RandomSpawn('n00N',1,GetRandomInt(0,3)) //Mass HP Zombie
        call RandomSpawn('uabo',1,GetRandomInt(0,udg_Players)) //Flesh Composite
        call PolledWait(20.0*udg_Pace)
        call FogTransition(0.3)
        call RemoveLocation(udg_CoreObjectivePoint)
        set udg_CoreObjectivePoint = GetUnitLoc(udg_Hero[udg_Sorted[GetRandomInt(1,udg_Players)]])
        call RandomSpawn('u000',2,GetRandomInt(0,udg_Spawn)) //Futty
        call RandomSpawn('u00L',4,IMinBJ(5,udg_Players)) //Blink
        call GeneralText(6.0, "|cfff3ad00Major Jamie Cox|r : We got back here as soon as we refueled and rearmed. You're not alone now. We'll sweep the sector and take out what we can." )
        call RandomSpawn('n00C',1,2) //Female
        if udg_Players>2 then
            call GeneralText(5.0, "|cfff3ad00Marine|r : My boys and I are buying you a round if we make it out of this alive." )
        endif
        call C130Wave15.execute()
        if udg_HitsTaken<140 then
            call GankMarine(Player(13),'u004',6+udg_Spawn,1600,2000)
        else
            call GankMarine(Player(13),'n004',4+udg_Spawn,1600,2000)
        endif
        set i = GetRandomInt(1,udg_Players)
        if GetUnitTypeId(udg_Hero[udg_Sorted[i]])!='h00E' then
            call FieldChat(udg_Hero[udg_Sorted[i]], 5.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : I'm running low on mags." )
        endif
        call PolledWait(20.0*udg_Pace)
        call FogTransition(0.35)
        call GeneralText(2.5, "|cffcd950cCaptain Roth|r : We've established a dust-off point South East of the sector. Bring your whiskeys and kilos there." )
        call PolledWait(20.0*udg_Pace)
        call GeneralText(5.0, "|cffcd950cBattalion|r : We'll be conducting landings soon to send in more reinforcements." )
        call RemoveLocation(udg_CoreObjectivePoint)
        set udg_CoreObjectivePoint = GetUnitLoc(udg_Hero[udg_Sorted[GetRandomInt(1,udg_Players)]])
        call PolledWait(5.0*udg_Pace)
        call GeneralText(5.0, "|cffcd950cBattalion|r : We're not losing this sector to Zack. Standby for further updates." )
        call PolledWait(15.0*udg_Pace)
        set udg_Visibility = 75
        if udg_Players>0 then
            call ExecuteRegisteredFunction("Wave16a")
            //call Wave16a.execute()
        endif
    endfunction

    private function init takes nothing returns nothing
        call RegisterFunction("Wave15a", function Wave15a)
    endfunction

endlibrary